package it.slkfarm.android;


import java.util.Locale;

import it.slkfarm.bean.Farm;
import it.slkfarm.bean.Production;
import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class CropTabView extends TabActivity
{
	/**
	 * @uml.property  name="production"
	 * @uml.associationEnd  
	 */
	private Production production;
	/**
	 * @uml.property  name="tabhost"
	 * @uml.associationEnd  
	 */
	private TabHost tabhost;
	/**
	 * @uml.property  name="pkg"
	 */
	private String pkg;
	/**
	 * @uml.property  name="farm"
	 * @uml.associationEnd  
	 */
	private Farm farm;
	/**
	 * @uml.property  name="lang"
	 */
	private int lang;
	/**
	 * @uml.property  name="secretKey"
	 */
	private String secretKey;
	/**
	 * @uml.property  name="tf"
	 * @uml.associationEnd  
	 */
	private Typeface tf;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
    {
		super.onCreate(savedInstanceState);
		Intent i = getIntent();
		pkg = getPackageName();
		farm = (Farm) i.getSerializableExtra(pkg + ".farm");
        lang = i.getIntExtra(pkg + ".lang", 1);
        secretKey = i.getStringExtra(pkg + ".secretKey");
        Log.i("cropTab sc", secretKey);
        Resources res = this.getResources();
	    DisplayMetrics dm = res.getDisplayMetrics();
	    android.content.res.Configuration conf = res.getConfiguration();
		if (lang == 1)
		{
			Log.i("tabview", "1");
			conf.locale = Locale.getDefault();
			tf = Typeface.createFromAsset(getAssets(),"fonts/KMKDSP__.ttf");
		}
		else 
		{
			Log.i("tabview", "2");
			conf.locale = new Locale("si");
			tf = null;
		}
		res.updateConfiguration(conf, dm);
        
        setContentView(R.layout.crop_tabview);
        tabhost = getTabHost(); 
        production = new Production();
        production.setFarm(farm);
        setTabs();
    }
        
			
	private void setTabs() 
	{
		if (lang == 1)
		{
			addTab(R.string.vegetable, R.drawable.tab_vegetable, "Vegetable");
			addTab(R.string.fruit,R.drawable.tab_fruit, "Fruits");
			addTab(R.string.other,R.drawable.tab_other, "Others");
			addTab(R.string.summary,R.drawable.tab_summary, "Summary");
		}
		else
		{
			addTab(R.string.vegetable,R.drawable.tab_vegetable, "එළවළු");
			addTab(R.string.fruit,R.drawable.tab_fruit, "පළතුරු");
			addTab(R.string.other,R.drawable.tab_other, "වෙනත් ");
			addTab(R.string.summary,R.drawable.tab_summary, "Summary");
		}
		tabhost.setCurrentTab(0);
	}

	
	@SuppressLint("NewApi")
	private void addTab(int labelId, int drawableId, String type) 
	{
		Intent intent;
		if (!type.equalsIgnoreCase("Summary"))
		{
			intent = new Intent(CropTabView.this, CropList.class);
			intent.putExtra(pkg + ".type", type);
		}
		else
		{
			intent = new Intent(CropTabView.this, CropSummary.class);
			intent.putExtra(pkg + ".secretKey", secretKey);
		}
		TabHost.TabSpec spec = tabhost.newTabSpec("tab" + labelId);		
		View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
		TextView title = (TextView) tabIndicator.findViewById(R.id.tabtitle);
		title.setText(labelId);
		title.setTypeface(tf);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.tabicon);
		icon.setImageResource(drawableId);
		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabhost.addTab(spec);
		
	}
	
	

	/**
	 * @return
	 * @uml.property  name="production"
	 */
	public Production getProduction() 
	{
		return production;
	}

	/**
	 * @param production
	 * @uml.property  name="production"
	 */
	public void setProduction(Production production) 
	{
		this.production = production;
	}
}
